import './buy.scss'

import { Tabbar, TabbarItem, Badge, XButton, CheckIcon } from 'vux'

import { fetch, rap } from 'js/fetch.js'
import bus from 'js/bus.js'

import { cookie } from 'vux'
import utils from 'js/utils.js'

let url = {
    info: '/user/getUser.do',
    logout: '/user/logout.do',
    stationLetter: 'api/messages/unread-count',
    getVip: 'api/vips',
}
url = rap(url)

export default {
    props: {
        data: {
            require: true
        }
    },
    data() {
        return {
            mobile: '',
            isLogin: false,
            lerrerNumber: '0',
            token: '',
            timer: 10000,
            check: false,
            pledge:true,
            deposit:0
        }
    },
    computed:{
        isComputed:function(){
           if(!!this.pledge){
               return 1
           }else{
               return 0
           }
        }
    },
    created() {
        this.isLogin = utils.isLogin()
        console.log(this.data.type)


        //判断是否登录状态
        if (this.isLogin) {
            this.token = cookie.get('token')
            this.isExpired= window.localStorage.getItem('isExpired')
            this.deposit=Number(window.localStorage.getItem('deposit'))
            //console.log('deposit:'+this.deposit)
            //console.log('Expired:'+this.isExpired)
        }
    },
    methods: {
        buyMember(id) {
            let _this=this
        
            
            //console.log(window.localStorage.getItem('deposit'))
            //console.log(window.localStorage.getItem('isExpired'))
            //console.log(this.pledge)
            //console.log(this.isComputed)
      
            //检查是否交了押金
            if ((Number(window.localStorage.getItem('deposit'))) <= 0&&this.data.type!=2) {
              //console.log(window.localStorage.getItem('deposit'))
              //console.log(Number(window.localStorage.getItem('deposit')))
              this.$vux.confirm.show({
                title: '未交纳押金',
                content: '你未交纳押金，本次支付将支付套餐和押金费用',
                onShow() {
                  console.log('plugin show')
                },
                onHide() {
                  console.log('plugin hide')
                },
                onCancel() {
                  console.log('plugin cancel')
                },
                onConfirm() {
               //支付购买会员
               fetch('post', `${url.getVip}/${id}/buy`, {
            }, { 'headers': _this.token }).then(res => {
              if (res.status >= 200 && res.status <= 300) {
                //console.log(res.data)
                let json=JSON.parse(res.data.config)
                //console.log(json)
                WeixinJSBridge.invoke(
                  'getBrandWCPayRequest',json,
                  function(res){
                      if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                          // 使用以上方式判断前端返回,微信团队郑重提示：
                          // res.err_msg将在用户支付成功后返回
                          // ok，但并不保证它绝对可靠。
                          alert('支付成功')
                          window.location.href = './member.html'
                      }
                  }
              );
  
                // this.$vux.toast.show({
                //   text: '购买成功',
                //   type: 'success',
                //   onShow() {
                //     //console.log('Plugin: I\'m showing')
                //   },
                //   onHide() {
                //     window.location.href = './member.html'
                //     //console.log('Plugin: I\'m hiding')
                //   }
                // })
              } else {
                _this.$vux.toast.show({
                  text: res.data.message,
                  type: 'warn',
                  onShow() {
                    //console.log('Plugin: I\'m showing')
                  },
                  onHide() {
                    //console.log('Plugin: I\'m hiding')
                  }
                })
              }
            })
                }
              })
      
              return
            }
      
      
            this.$vux.confirm.show({
              title: '购买会员',
              content: '你确定要购买吗？',
              onShow() {
                console.log('plugin show')
              },
              onHide() {
                console.log('plugin hide')
              },
              onCancel() {
                console.log('plugin cancel')
              },
              onConfirm() {
                
                //支付购买会员
                fetch('post', `${url.getVip}/${id}/buy`, {
                }, { 'headers': _this.token }).then(res => {
                  if (res.status >= 200 && res.status <= 300) {
                    //console.log(res.data)
                    let json=JSON.parse(res.data.config)
                    //console.log(json)
                    WeixinJSBridge.invoke(
                      'getBrandWCPayRequest',json,
                      function(res){
                          if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                              // 使用以上方式判断前端返回,微信团队郑重提示：
                              // res.err_msg将在用户支付成功后返回
                              // ok，但并不保证它绝对可靠。
                              alert('支付成功')
                              window.location.href = './member.html'
                          }
                      }
                  );
      
                    // this.$vux.toast.show({
                    //   text: '购买成功',
                    //   type: 'success',
                    //   onShow() {
                    //     //console.log('Plugin: I\'m showing')
                    //   },
                    //   onHide() {
                    //     window.location.href = './member.html'
                    //     //console.log('Plugin: I\'m hiding')
                    //   }
                    // })
                  } else {
                    _this.$vux.toast.show({
                      text: res.data.message,
                      type: 'warn',
                      onShow() {
                        //console.log('Plugin: I\'m showing')
                      },
                      onHide() {
                        //console.log('Plugin: I\'m hiding')
                      }
                    })
                  }
                })
              }
            })
          } 
    },
    components: {
        Tabbar,
        TabbarItem,
        Badge,
        XButton,
        CheckIcon
    }
}
